import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/***
 * 
 * read the parameter from a given file and and stop their data in data
 * structure
 * 
 */
public class ParametersHelper {
	private Map<String, String> mParameters;
	Boolean mIs_using_improved_algorithm = false;
	Boolean in_comparing_mode = false;
	RankedResults gt = null;
	Writer writer = null;
	Vector<String> StopWordList = null;

	public ParametersHelper(String fileName) throws IOException {
		String line;
		File fin = new File(fileName);
		FileInputStream fileInputStream = new FileInputStream(fin);
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(fileInputStream));
		mParameters = new HashMap<String, String>();

		while ((line = bufferedReader.readLine()) != null) {
			String[] parts = line.split("=");
			mParameters.put(parts[0], parts[1]);
		}

		bufferedReader.close();

		ReadAlgorithm();
		ReadGT();
		extract_stopword_from_file();
	}

	private void ReadAlgorithm() {
		if (getParametersMap().get("retrievalAlgorithm").compareTo("basic") == 0) {
			mIs_using_improved_algorithm = false;
		} else {
			mIs_using_improved_algorithm = true;
		}
	}

	private void ReadGT() throws NumberFormatException, IOException {
		if (getParametersMap().containsKey("truth")) {
			in_comparing_mode = true;
			String gt_file_name = getParametersMap().get("truth");
			gt = extract_gt_from_file(gt_file_name);
		}
	}

	public RankedResults extract_gt_from_file(String gt_file_name)
			throws NumberFormatException, IOException {
		String line;
		File fin = new File(gt_file_name);
		FileInputStream fileInputStream = new FileInputStream(fin);
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(fileInputStream));

		// query id, docs id
		RankedResults gt_with_rank = new RankedResults();

		while ((line = bufferedReader.readLine()) != null) {
			String[] parts = line.split(" ");
			int query_id = Integer.parseInt(parts[0]);
			int doc_id = Integer.parseInt(parts[2]);
			int rank = Integer.parseInt(parts[4]);
			gt_with_rank.AddResults(query_id, doc_id, rank);
		}

		bufferedReader.close();
		return gt_with_rank;
	}

	public Vector<String> extract_stopword_from_file()
			throws NumberFormatException, IOException {
		String stopListFile = getParametersMap().get("stopListFile");
		stopListFile = (stopListFile == null) ? "stopwords.txt" : stopListFile;

		String line;
		File fin = new File(stopListFile);
		FileInputStream fileInputStream = new FileInputStream(fin);
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(fileInputStream));

		StopWordList = new Vector<String>();

		while ((line = bufferedReader.readLine()) != null) {
			StopWordList.add(line.trim());
		}

		bufferedReader.close();
		return StopWordList;
	}

	Map<String, String> getParametersMap() {
		return mParameters;
	}
}